const {
    CombinedPredicate: {
        not,
    },
    stringValidator: {
        isDomain,
    },
    compare,
} = rx
const validate = (() => {
    const notDomain = not(isDomain)

    return RxFormValidator.validator({
        rxElemValidators: [
            RxElemValidator.notBlank('domain', '[域名] 不能为空'),

            RxElemValidator.create('domain', '[域名] 格式错误，示例: abc.com', notDomain),
        ],
    })
})()

class SysMediaTrustDomains {
    constructor({
                    domain = '',
                    createTime = '',
                } = {}) {
        this.domain = domain
        this.createTime = createTime
    }
}

const app = new BasicTableVue({
    data() {
        return {
            api: sysMediaTrustDomainsApi,
            formData: new SysMediaTrustDomains(),
            dialogShow: false,
            dtitle: '',
            formLabelWidth: '50px',
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({prop: 'domain', title: '域名'}),
                new TableColumn({prop: 'createTime', title: '添加时间', formatter: TableColumn.datetimeFormat}),
                new TableColumn({prop: 'operating', title: '操作', customComponent: true}),
            ],
        }
    },
    methods: {
        createForm() {
            return new SysMediaTrustDomainsVo()
        },
        addInfo() {
            this.dtitle = '新增'
            this.dialogShow = true
            this.formData = new SysMediaTrustDomains()
        },
        updateInfo(row) {
            this.dtitle = '修改'
            var cloneDeep = _.cloneDeep(row)
            cloneDeep.priority = parseInt(cloneDeep.priority, 2)
            this.formData = cloneDeep
            this.dialogShow = true
        },
        async addData() {
            const formData = this.formData
            if (!(await validate(this.formData))) {
                return
            }
            var res;
            formData.createTime = new Date()
            //新增
            res = await sysMediaTrustDomainsApi.add(formData)
            const {msg = '', code = 0} = res;
            if (code === 200) {
                await Promise.all([
                    rxPrompt.successMsg(this.dtitle + '成功'),
                    this.searchPage(),
                ])
                this.dialogShow = false
            } else {
                await rxPrompt.dangerMsg(this.dtitle + `失败,${msg}`);
            }
        },
        async del(row) {
            if (!row.domain) {
                await rxPrompt.msg('操作失败')
                return
            }
            const res = await rxPrompt.confirm('你确认要删除该信任域名吗?？', ['确认', '取消'])
            if (!res) {
                return
            }
            const result = await this.api.deleteByDomain(row.domain)
            if (!result) {
                return
            }
            await Promise.all([
                rxPrompt.successMsg('删除成功'),
                this.searchPage(),
            ])
        },
        selection (data) {
            this.selectedIdList = data.map(({domain}) => domain)
            this.selectedList = data
        },
        async deleteSelected () {
            if (this.checkSelected() && this.checkSelectedReport()) {
                const res = await rxPrompt.confirm('你确定要删除吗？', ['确认', '取消'])
                if (res) {
                    const result = await this.api.deleteBatchDomains(this.selectedIdList)
                    if (!result) {
                        await rxPrompt.msg(`删除失败: ${result.msg}`)
                        return
                    }
                    // noinspection JSIgnoredPromiseFromCall
                    rxPrompt.msg('删除成功')
                    this.page.current = 1
                    await this.searchPage()
                }
            }
        },
        async downloadTemplate() {
            await sysMediaTrustDomainsApi.downloadTemplate('多媒体域名信任表导入模板.xlsx')
        },
        async init() {

        },
    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
})
